package com.example.fresh_market.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.request.RequestOptions;
import com.example.fresh_market.activity.AccountSettingActivity;
import com.example.fresh_market.activity.AddressActivity;
import com.example.fresh_market.activity.LoginActivity;
import com.example.fresh_market.constants.StrConstant;
import com.example.fresh_market.databinding.FragmentMineBinding;
import com.example.fresh_market.entity.UserInfo;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.utils.HttpUtil;
import com.example.fresh_market.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class MineFragment extends Fragment {

    private FragmentMineBinding binding;
    private Bundle bundle;
    private Context mContext;

    public static Fragment newInstance() {
        return new MineFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = FragmentMineBinding.inflate(getLayoutInflater());
        bundle = new Bundle();
        mContext = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        setComponentEvent();
        init();
        return binding.getRoot();
    }

    public void init() {
        SharedPreferences preferences = mContext.getSharedPreferences(StrConstant.CONFIG, Context.MODE_PRIVATE);
        String token = preferences.getString(StrConstant.TOKEN, "");
        HttpUtil.get(mContext, "/user", Map.of(HttpUtil.Auth, token), response -> {
            ApiResponse<UserInfo> apiResponse = new Gson().fromJson(response, new TypeToken<ApiResponse<UserInfo>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                UserInfo userInfo = apiResponse.getData();
                binding.userName.setText(String.format("用户名：%s", userInfo.getNickname()));
                binding.userEmail.setText(String.format("邮箱：%s", userInfo.getEmail()));
                binding.userPhone.setText(String.format("电话：%s", userInfo.getPhone()));
                if (StringUtils.isNotBlank(userInfo.getAvatar())) {
                    HttpUtil.loadImage(binding.userAvatar, userInfo.getAvatar(), new RequestOptions());
                }
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            init();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    private void setComponentEvent() {
        binding.layoutAddressManagement.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, AddressActivity.class);
            startActivity(intent, bundle);
        });
        binding.layoutOrderManagement.setOnClickListener(v -> {
            ToastUtil.showToast(mContext, "订单管理");
        });
        binding.layoutHelpFeedback.setOnClickListener(v -> {
            ToastUtil.showToast(mContext, "帮助与反馈");
        });
        binding.layoutAccountSettings.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, AccountSettingActivity.class);
            startActivity(intent, bundle);
        });
        binding.btnLogout.setOnClickListener(v -> {
            SharedPreferences preferences = mContext.getSharedPreferences(StrConstant.CONFIG, Context.MODE_PRIVATE);
            preferences.edit().clear().apply();
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent, bundle);
            getActivity().finish();
        });
    }
}
